package com.jimo.ads;

import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import com.jimo.ads.databinding.ActivityMainBinding;
import com.qq.e.ads.banner2.UnifiedBannerADListener;
import com.qq.e.ads.banner2.UnifiedBannerView;
import com.qq.e.ads.rewardvideo.RewardVideoAD;
import com.qq.e.ads.rewardvideo.RewardVideoADListener;
import com.qq.e.comm.util.AdError;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private UnifiedBannerView banner;
    final String TAG_BANNER ="emmmmmmmmmm(banner):";
    final String TAG_REWARD ="emmmmmmmmmm(reward):";
    private RewardVideoAD rewardVideoAD;
    private ListAdapter listAdapter;
    private final List<ItemBean> list=new ArrayList<>();
    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initBanner();
        initReward();
        initClick();
        initList();
    }

    private void initList() {
        listAdapter=new ListAdapter(this,R.layout.item_view,list);
        list.add(new ItemBean());
        list.add(new ItemBean());
        list.add(new ItemBean());
        list.add(new ItemBean());
        list.add(new ItemBean());
        binding.list.setAdapter(listAdapter);
        listAdapter.notifyDataSetChanged();
    }

    private void initClick() {
        binding.btn.setOnClickListener(view -> {
            binding.container.removeAllViews();
            binding.container.addView(banner);
            banner.loadAD();
        });
        binding.jili.setOnClickListener(view -> rewardVideoAD.loadAD());
    }

    private void initReward() {
        rewardVideoAD = new RewardVideoAD(this, "ID3094656618703713", new RewardVideoADListener() {
            @Override
            public void onADLoad() {
                Log.i(TAG_REWARD, "onADLoad: ");
                rewardVideoAD.showAD(MainActivity.this);
            }

            @Override
            public void onVideoCached() {
                Log.i(TAG_REWARD, "onVideoCached: ");
            }

            @Override
            public void onADShow() {
                Log.i(TAG_REWARD, "onADShow: ");
            }

            @Override
            public void onADExpose() {
                Log.i(TAG_REWARD, "onADExpose: ");
            }

            @Override
            public void onReward(Map<String, Object> map) {
                Log.i(TAG_REWARD, "onReward: ");
            }

            @Override
            public void onADClick() {
                Log.i(TAG_REWARD, "onADClick: ");
            }

            @Override
            public void onVideoComplete() {
                Log.i(TAG_REWARD, "onVideoComplete: ");
            }

            @Override
            public void onADClose() {
                Log.i(TAG_REWARD, "onADClose: ");
            }

            @Override
            public void onError(AdError adError) {
                Log.i(TAG_REWARD, "onError: ");
            }
        }); // 有声播放
    }

    private void initBanner() {
        banner=new UnifiedBannerView(this, "2014155688107233", new UnifiedBannerADListener() {
            @Override
            public void onNoAD(AdError adError) {
                Log.i(TAG_BANNER, "onNoAD: "+adError.getErrorMsg());
                Log.i(TAG_BANNER, "onNoAD: "+adError.getErrorCode());
            }

            @Override
            public void onADReceive() {
                Log.i(TAG_BANNER, "onADReceive: ");
            }

            @Override
            public void onADExposure() {
                Log.i(TAG_BANNER, "onADExposure: ");
            }

            @Override
            public void onADClosed() {
                Log.i(TAG_BANNER, "onADClosed: ");
            }

            @Override
            public void onADClicked() {
                Log.i(TAG_BANNER, "onADClicked: ");
            }

            @Override
            public void onADLeftApplication() {
                Log.i(TAG_BANNER, "onADLeftApplication: ");
            }
        });
        binding.container.addView(banner);
    }

}